/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import com.sun.net.ssl.internal.ssl.Debug;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author Alfred
 */
public class product {

    private int productNumber;
    private String productName;
    private float price;
    private int startingStock;
    private int stockAdd;
    private int stockMinus;
    private boolean isGasul;
    private boolean isActive;
    private Date maxInventoryDate;
    private Date stockDate;

    public product(int productNumber, String productName, float price, int stock, int stockAdd, int stockMinus, boolean isGasul, boolean isActive, Date stockDate) {
        this.productNumber = productNumber;
        this.productName = productName;
        this.price = price;
        this.startingStock = stock;
        this.stockAdd = stockAdd;
        this.stockMinus = stockMinus;
        this.isGasul = isGasul;
        this.isActive = isActive;
        this.stockDate = stockDate;

        addStockRow();
    }

    public int getProductNumber() {
        return this.productNumber;
    }

    public String getProductName() {
        return this.productName;
    }

    public float getPrice() {
        return this.price;
    }

    public int getStock() {
        return this.startingStock;
    }

    public int getCurrentStock() {
        return getStock() + getStockAdd() - getStockMinus();
    }

    public int getStockAdd() {
        return this.stockAdd;
    }

    public Date getStockDate() {
        return this.stockDate;
    }

    public int getStockMinus() {
        int tempStockMinus = this.stockMinus;
        Calendar nextDay = new GregorianCalendar();
        nextDay.setTime(getStockDate());
        nextDay.add(Calendar.DATE, 1);
        String nextDayString = new SimpleDateFormat("yyyy-MM-dd").format(nextDay.getTime());
        
        String sqlStmt = "select * from `transaction`"
                    + " left join `purchasedproduct` on `transaction`.`transactionNumber` = `purchasedProduct`.`transactionNumber`"
                    + " where `purchasedProduct`.`productNumber` = " + Integer.toString(getProductNumber())
                    + " and `transaction`.`dateAndTime` >= "
                    + String.format("'%s'  and `transaction`.`dateAndTime` < '%s'", getStockDate(), nextDayString);
        try {
            databaseConn conn = new databaseConn();
            ResultSet inventoryAtCurrentDate = conn.sqlRead(sqlStmt);
            while(inventoryAtCurrentDate.next()) {
                tempStockMinus += inventoryAtCurrentDate.getInt("quantity");
            }
        } catch (SQLException ex) {
            return this.stockMinus;
        }
        return tempStockMinus;
    }

    public boolean getIsGasul() {
        return this.isGasul;
    }

    public boolean getIsActive() {
        return this.isActive;
    }

    public Date getMaxInventoryDate() {
        return this.maxInventoryDate;
    }

    public boolean setIsActive(boolean isActive) {
        databaseConn conn = new databaseConn();
        if (conn.sqlStatement(String.format("UPDATE `sashmirgasul`.`product` SET `isActive`=%d WHERE `productNumber`='%d';", isActive ? 1 : 0, this.getProductNumber()))) {
            this.isActive = isActive;
            return true;
        } else {
            return false;
        }
    }

    public boolean setProductName(String productName) {
        databaseConn conn = new databaseConn();
        if (conn.sqlStatement(String.format("UPDATE `sashmirgasul`.`product` SET `productName`='%s' WHERE `productNumber`='%d';", productName, this.getProductNumber()))) {
            this.productName = productName;
            return true;
        } else {
            return false;
        }
    }

    public boolean setProductPrice(float price) {
        databaseConn conn = new databaseConn();
        if (conn.sqlStatement(String.format("INSERT INTO `sashmirgasul`.`productpricehistory` (`dateAndTime`, `productNumber`, `price`) VALUES (NOW(), %d, %f);", this.getProductNumber(), price))) {
            this.price = price;
            return true;
        } else {
            return false;
        }
    }

    public final void addStockRow() {
        getMaxDateFromDB();

        String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new GregorianCalendar().getTime());
        String maxDateString = getMaxInventoryDate() != null ? getMaxInventoryDate().toString() : "";

        if (((getMaxInventoryDate() == null) || !(currentDateString.equals(maxDateString))) && this.isActive) {
            this.startingStock = getCurrentStock();
            this.stockAdd = 0;
            this.stockMinus = 0;
            databaseConn conn = new databaseConn();
            conn.sqlStatement(String.format("INSERT INTO `sashmirgasul`.`inventory` (`dateAndTime`, `productNumber`, `startingStock`, `add`, `less`) VALUES (NOW(), %d, %d, 0, 0);", this.getProductNumber(), this.startingStock));
        }
    }

    public void getMaxDateFromDB() {
        this.maxInventoryDate = new Date();

        databaseConn conn = new databaseConn();
        ResultSet maxDateRead = conn.sqlRead(String.format("SELECT MAX(`dateAndTime`) as maxDate from inventory WHERE productNumber = %d", this.productNumber));
        try {
            maxDateRead.next();
            maxInventoryDate = maxDateRead.getDate("maxDate");
        } catch (SQLException ex) {
        }
    }

    public void updateProductStock(int newStock) {

        if (getCurrentStock() > newStock) {
            this.stockMinus += getCurrentStock() - newStock;
            //Insert Minus SQL Statement
            databaseConn conn = new databaseConn();
            conn.sqlStatement(String.format("UPDATE `sashmirgasul`.`inventory` SET `less`= %d WHERE `dateAndTime`= '%s' and`productNumber`= '%d';", this.stockMinus, getMaxInventoryDate().toString(), this.productNumber));
        } else if (getCurrentStock() < newStock) {
            this.stockAdd += newStock - getCurrentStock();
            //Insert Add SQL Statement
            databaseConn conn = new databaseConn();
            conn.sqlStatement(String.format("UPDATE `sashmirgasul`.`inventory` SET `add`= %d WHERE `dateAndTime`= '%s' and`productNumber`= '%d';", this.stockAdd, getMaxInventoryDate().toString(), this.productNumber));
        }
    }
}
